package ru.incoremedia.gwttest.application.client;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.rpcforms.client.handler.AsyncResultHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import ru.incoremedia.gwttest.application.client.action.GetAuthInfoAction;
import ru.incoremedia.gwttest.application.client.dto.AuthInfo;
import ru.incoremedia.gwttest.application.client.view.guest.layout.GuestLayout;
import ru.incoremedia.gwttest.application.client.view.user.layout.UserLayout;
import ru.incoremedia.gwttest.core.client.component.ajaxloaderindicator.AjaxLoaderIndicator;
import ru.incoremedia.gwttest.core.client.security.UserRole;

public class ApplicationEntryPoint implements EntryPoint {
    
    @Override
    public void onModuleLoad() {
        AjaxLoaderIndicator.showModal();
        Log.setUncaughtExceptionHandler();
           
        DeferredCommand.addCommand(new Command() {
            @Override
            public void execute() {
                onModuleLoad2();
            }
        });
    }

    public void onModuleLoad2() {
        new GetAuthInfoAction().execute(new AsyncResultHandler<AuthInfo>() {
            @Override
            public void onSuccess(AuthInfo authInfo) {
                if(authInfo.getUserRole().equals(UserRole.ROLE_GUEST)) {
                    GuestLayout.showAsync();
                }

                if(authInfo.getUserRole().equals(UserRole.ROLE_USER) || authInfo.getUserRole().equals(UserRole.ROLE_ADMIN)) {
                    UserLayout.showAsync();
                }
            }
        });
    }
    
}